package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.domain.dto.DictTypeDto;
import com.jvyou.app.common.domain.entity.DictType;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.service.DictTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典类型表(DictType)表控制层
 *
 * @author 橘柚
 * @since 2025-03-13 10:10:11
 */
@RestController
@RequestMapping("/dictType")
public class DictTypeController  {
    
    @Resource
    private DictTypeService dictTypeService;

    /**
     * 查询全部数据
     *
     * @return 列表
     */
    @GetMapping("/list")
    @SaCheckPermission("system:dictType:list")
    public Result<List<DictType>> list(@ModelAttribute DictTypeDto dictTypeDto) {
        return Result.success(dictTypeService.listDictType(dictTypeDto));
    }


    /**
     * 分页查询数据
     *
     * @return 列表
     */
    @GetMapping("/page")
    @SaCheckPermission("system:dictType:page")
    public Result<PageResult<DictType>> page(@ModelAttribute DictTypeDto dictTypeDto) {
        return Result.success(dictTypeService.pageDictType(dictTypeDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:dictType:get")
    public Result<DictType> selectOne(@PathVariable Long id) {
        return Result.success(dictTypeService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dictType 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:dictType:insert")
    public Result<DictType> insert(@RequestBody DictType dictType) {
        boolean success = dictTypeService.save(dictType);
        return success ? Result.success(dictType) : Result.error(HttpCodeEnum.ADD_FAIL);
    }
    
     /**
     * 修改数据
     *
     * @param dictType 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("system:dictType:update")
    public Result<DictType> updateById(@RequestBody DictType dictType) {
        boolean success = dictTypeService.updateById(dictType);
        return success ? Result.success(dictType) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }
    

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:dictType:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = dictTypeService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }
    
}

